package ee.parameterConverter.impl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.Map;

import ee.metadataUtils.ClassUtils;
import ee.parameterConverter.TypeKind;
import ee.parameterConverter.TypeKindResolver;

public class TypeKindResolverImpl implements TypeKindResolver {
	
	@Override
	public TypeKind resolve(Type type)
	{
		Type rawType = type;
		
		if (type instanceof ParameterizedType)
		{
			rawType = ((ParameterizedType) type).getRawType();
		}
		
		Class<?> typeClass = (Class<?>) rawType;
		
		if (ClassUtils.isPrimitive(typeClass) 
				|| ClassUtils.isWrapper(typeClass)
				|| typeClass.equals(String.class) 
				|| typeClass.equals(Date.class)
				|| (typeClass.isArray() && (Character[].class.isAssignableFrom(typeClass)) || char[].class.isAssignableFrom(typeClass) )
				|| Enum.class.isAssignableFrom(typeClass))
		{
			return TypeKind.Simple;
		} else if (typeClass.isArray() || List.class.isAssignableFrom(typeClass))
		{
			return TypeKind.List;
		} else if (Map.class.isAssignableFrom(typeClass))
		{
			return TypeKind.Map;
		} else
		{
			return TypeKind.Complex;
		}
	}
}
